package top.aigoo.project01.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.aigoo.project01.cache.JedisUtil;
import top.aigoo.project01.entity.ShopCategory;
import top.aigoo.project01.mapper.ShopCategoryMapper;
import top.aigoo.project01.service.ShopCategoryService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 店铺分类
 * @Author: Mr.OYSHUO.欧阳大叔  mail:1642302522@qq.com
 * @Version 1.0.0
 * @version 2.0  引入了redis
 * @Date: 2021/8/12 11:17
 */
@Service
public class ShopCategoryServiceImpl implements ShopCategoryService {
    @Autowired
    private ShopCategoryMapper shopCategoryMapper;
    @Autowired
    private JedisUtil.Keys jedisKeys;
    @Autowired
    private JedisUtil.Strings jedisStrings;

    private Logger logger = LoggerFactory.getLogger(ShopCategoryServiceImpl.class);


    /**
     * 获取店铺分类
     *
     * @param shopCategoryCondition
     * @return
     */
    @Override
    public List<ShopCategory> getShopCategoryList(ShopCategory shopCategoryCondition) {
        //定义redis的key的前缀
        String key = SCLISTKEY;
        //定义接收对象
        List<ShopCategory> shopCategoryList = null;
        //定义jackson数据转换操作类
        ObjectMapper mapper = new ObjectMapper();


        //根据传入的条件,拼接出redis的key,可能三种，分别为
        // shopcategorylist_allfirstlevel
        // shopcategorylist_parent_?、
        // shopcategorylist_allsecondlevel
        if (shopCategoryCondition == null) {
            //如果查询条件为空，就列出来所有的首页大类，既parentId为空的店铺类别
            key = key + "_allfirstlevel";
        } else if (shopCategoryCondition != null
                && shopCategoryCondition.getParent() != null
                && shopCategoryCondition.getParent().getShopCategoryId() != null) {
            //若parentId为非空，则列出该ParentId下所有子类别
            key = key + "_parent" + shopCategoryCondition.getParent().getShopCategoryId();
        } else if (shopCategoryCondition != null) {
            //列出所有子类别，不管其属于哪个类，都列出来
            key = key + "_allsecondlevel";
        }

        //判断key是否存在
        if (!jedisKeys.exists(key)) {
            //若不存在，则从数据库里面取出相应数据
            shopCategoryList = shopCategoryMapper.queryShopCategory(shopCategoryCondition);
            //将相关的实体类集合转换成string,存入redis里面对应的key中
            String jsonString = null;
            try {
                jsonString = mapper.writeValueAsString(shopCategoryList);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            }
            jedisStrings.set(key, jsonString);
        } else {
            //如果存在，直接从redis里面取出相应数据
            String jsonString = jedisStrings.get(key);
            //指定要将string转换成集合类型
            JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class,
                    ShopCategory.class);
            try {
                shopCategoryList = mapper.readValue(jsonString, javaType);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            }
        }
        return shopCategoryList;
    }
}
